package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
public class DeptController {

    //private static final Logger log= LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService  deptService;

    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    public Result list(){
        //System.out.println("查询全部的部门数据");
        log.info("查询全部的部门数据");
        List<Dept> deptList=deptService.findAll();
        return Result.success(deptList);
    }

//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idStr=request.getParameter("id");
//        int id=Integer.parseInt(idStr);
//        return Result.success();
//    }

    @Log
    @DeleteMapping("/depts")
    public Result delete( Integer id){
        //System.out.println("删除部门id为："+id);
        log.info("删除部门id为："+id);
        deptService.deleteById(id);
        return Result.success();
    }

    @Log
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        //System.out.println("添加部门："+dept);
        log.info("添加部门："+dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result getInfo(@PathVariable/*("id")*/ Integer id){
        //System.out.println("查询部门id为："+id);
        log.info("查询部门id为："+id);
        Dept dept=deptService.getInfo(id);
        return Result.success(dept);
    }


    @Log
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门："+dept);
        log.info("修改部门："+dept);
        deptService.update(dept);
        return Result.success();
    }
}
